package it.xiuxian.xingqiu.activity

import android.app.AlertDialog
import android.os.Build
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.youth.banner.BannerConfig
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.utils.GlideRoundImageLoader
import it.xiuxian.lib.utils.ImageUtil
import it.xiuxian.lib.utils.ToastUtil
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.xingqiu.R
import it.xiuxian.xingqiu.databinding.BannerActivityBinding
import it.xiuxian.xingqiu.presenter.BannerActivityPresenter
@Route(path = ArouterAddress.BANNERACTIVITY)
class BannerActivity:BaseActivity<BannerActivityPresenter>() {
    @JvmField
    @Autowired
    var newlist:ArrayList<String>?=null
    @JvmField
    @Autowired
    var id:String?=null
    lateinit var adapter1: SlimAdapter
    lateinit var binding:BannerActivityBinding
    override val layoutId: Int
        get() = R.layout.banner_activity
    override val presenter: BannerActivityPresenter
        get() = BannerActivityPresenter()
    var i=0
    var currentIndex=-1
    @RequiresApi(Build.VERSION_CODES.Q)
    override fun initView(){
        ARouter.getInstance().inject(this)
        binding= getViewDataBinding<BannerActivityBinding>()
      /* val manager= LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.HORIZONTAL
        binding.rv.layoutManager = manager*/
        binding.tvGongxian.setOnClickListener {
            var dialog = AlertDialog.Builder(this)
            var view=  View.inflate(this,R.layout.alert_dialog,null)
            var editText =  view.findViewById<EditText>(R.id.et_num)
            editText.setTextCursorDrawable(getDrawable(R.drawable.lib_cursor_color))
            dialog.setView(view).create()
            var show =  dialog.show()
            var d =  windowManager.defaultDisplay
            show.window?.setLayout((d.width*0.8).toInt(), LinearLayout.LayoutParams.WRAP_CONTENT)
            show.window?.setBackgroundDrawableResource(R.drawable.lib_shape_corner)
            view.findViewById<TextView>(R.id.tv_sure).setOnClickListener {
                if(editText.text.isNotEmpty()){
                    show.dismiss()
                    mPresenter!!.submit(editText.text.toString(),id!!)
                }else{
                    ToastUtil.showShort("请输入数量")
                }

            }
            view.findViewById<TextView>(R.id.tv_cancel).setOnClickListener{
                show.dismiss()
            }
        }
       /* adapter1 = SlimAdapter.create().register<String>(
            R.layout.fragment_banner_item,
            object : SlimInjector<String>{
                override fun onInject(
                    data: String?,
                    injector: IViewInjector<out IViewInjector<*>>?
                ) {
                   // injector!!.text(R.id.tv_xiabiao,(++i).toString()+"/"+newlist!!.size.toString())
                    ImageUtil.setRoundImage(data, injector!!.findViewById<ImageView>(R.id.imageview))


                }
            }).attachTo(binding.rv).updateData(newlist)
        binding.rv.addOnScrollListener(object:RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
               // super.onScrolled(recyclerView, dx, dy)
                if(newlist!!.size<2){
                    return
                }
                var firstReal =   manager.findFirstVisibleItemPosition()
                var viewFirst =    manager.findViewByPosition(firstReal)
              var width =  windowManager.defaultDisplay.width
                if(width!=0&&viewFirst!=null){
                   var radio= viewFirst.right/width
                    if(radio>0.8){
                            if(currentIndex!=firstReal){
                                currentIndex = firstReal
                                refreshIndicator()
                            }
                        }else if(radio<0.2){
                            if (currentIndex != firstReal + 1) {
                                currentIndex = firstReal + 1
                                refreshIndicator()
                            }
                        }

                }

            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int){
                //连续滑动时可能不会回调
                val first: Int = manager.findFirstVisibleItemPosition()
                val last: Int = manager.findLastVisibleItemPosition()
                if (currentIndex != first && first == last) {
                    currentIndex = first
                    refreshIndicator()
                }
            }
        })*/
       // binding.rv.setOnScrollChangeListener(View.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->  })
        binding.banner.setImageLoader(GlideRoundImageLoader())
        binding.banner.setDelayTime(2000)
        binding.banner.setBannerStyle(BannerConfig.NUM_INDICATOR)
        binding.banner.setImages(newlist)
        binding.banner.start()

    }

    private fun refreshIndicator() {
           // binding.tvXiabiao.text = i.toString()+"/"+newlist!!.size.toString()
    }

    override fun initEvent(){
        binding.llBack.setOnClickListener{
            finish()
        }
    }
    fun setdata(msg: String){
        ToastUtil.showShort(msg)
    }
}